import {BASEURL} from "./config.js"
const request=(method,url,data={})=>{
	let token = uni.getStorageSync("token")
	if(!token){
		uni.navigateTo({
			url:'/pages/login/login'
		})
	}
	console.log(BASEURL+url)
	//异步请求, 
	
	return new Promise((resolve,reject)=>{
		 uni.showLoading({
		 	 title: '加载中...'
		 })
		 	uni.request({
		 	    url:BASEURL+url, 
		 	    data: data,
		 		method:method,
		 	    header: {
					'Content-Type': 'application/json;charset=UTF-8',
		 	        'Authorization': token //自定义请求头信息
		 	    },
		 	    success: (res) => {
		 	        
					resolve(res.data)
					// console.log(res.data.errMsg)
					if(res.data.errCode!=10000){
						uni.showToast({
						title:res.data.errMsg
					})
					
					}
					uni.hideLoading()
		 	    },
				fail:(err)=>{
					reject(err)
					console.log(err.data.errMsg)
					
				}
		 	});
			
	 })
	
	 

	
}
const get=(url,data)=>{
	
	return request("GET",url,data)
}
const post=(url,data)=>{
	
	return request("POST",url,data)
}
export{
	get,post
}